import {createRouter,createWebHashHistory} from 'vue-router'
import { isMobile } from '/src/utils/mobile.js'
import { getPageTitle } from '/src/utils/getPageTitle.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import {onMounted} from 'vue'

const wap_router = [{
	path: 'login',
	name:'home_login',
	meta:{
		title:'求职登录',
		keepAlive: false,
		tabBar:false,
		navBar:{
			state:false,
			leftBtn:false,
		},
	},
	component: ()=> import("../views/home/login.vue") 
},{
	path: 'register',
	name:'home_register',
	meta:{
		title:'求职注册',
		keepAlive: false,
		tabBar:false,
		navBar:{
			state:true,
			leftBtn:true,
		},
	},
	component: ()=> import("../views/home/register.vue") 
},{
	path: 'index',
	name:'home',
	meta:{
		title:'首页',
		keepAlive: false,
		tabBar:true,
		navBar:{
			state:true,
			leftBtn:false,
		},
	},
	component:()=> import("../views/home/home.vue")
},{
	path: 'hoves',
	name:'hoves',
	meta:{
		title:'有了',
		keepAlive: false,
		tabBar:true,
		navBar:{
			state:true,
			leftBtn:true,
		},
	},
	component:()=> import("../views/home/hoves.vue")
},{
	path: 'message',
	name:'message',
	meta:{
		title:'消息',
		keepAlive: false,
		tabBar:true,
		navBar:{
			state:true,
			leftBtn:false,
		},
	},
	component:()=> import("../views/home/message.vue")
},{
	path: 'mine',
	name:'mine',
	meta:{
		title:'我的',
		keepAlive: false,
		tabBar:true,
		navBar:{
			state:true,
			leftBtn:false,
		},
	},
	component:()=> import("../views/home/mine.vue")
},{
	path:'card_details',
	name:'cardDetails',
	meta:{
		title:'职位详情',
		keepAlive: false,
		tabBar:false,
		navBar:{
			state:true,
			leftBtn:true,
		},
	},
	component:()=> import("../views/home/hoves.vue")
},{
	path:'chat',
	name:'chat',
	meta:{
		title: '沟通',
		keepAlive: false,
		tabBar:false,
		navBar:{
			state:false,
			leftBtn:true,
		},
	},
	component:()=> import("../views/home/chat.vue")
},{
	path:'search',
	name:'search',
	meta:{
		title:'搜索',
		keepAlive: true,
		tabBar:true,
		navBar:{
			state:false,
			leftBtn:true,
		},
	},
	component:()=> import("../views/home/search.vue")
}]

const routes = [
	{//移动端路由
	path: '/home',
	redirect:'/home/index',
	meta:{title:'首页'},
	component:()=> import("../views/home/index.vue"),
	children: wap_router
	},{
		path: '/enterprise',
		redirect:'/enterprise/index',
		meta:{title:'企业端'},
		component: ()=> import("../views/enterprise/index.vue"),
		children:[{
				path: 'index',
				name:'enterprise',
				meta:{title:'企业端首页'},
				component: ()=> import("../views/enterprise/home.vue")
			},{
				path: 'job',
				name:'job',
				meta:{title:'岗位管理'},
				component: ()=> import("../views/enterprise/job.vue")
			},{
				path: 'messge',
				name:'messge',
				meta:{title:'消息管理'},
				component: ()=> import("../views/enterprise/messge.vue")
			}]
	},{
		path: '/enterprise/login',
		name:'enterprise_login',
		meta:{title:'企业端'},
		component: ()=> import("../views/enterprise/login.vue") 
	},{
		path: '/enterprise/register',
		name:'enterprise_register',
		meta:{title:'企业端注册'},
		component: ()=> import("../views/enterprise/register.vue") 
	},{//管理员路由
		path: '/admin',
		name:'admin',
		redirect:'/admin/index',
		meta:{title:'管理端首页'},
		component: ()=> import("../views/admin/index.vue"),
		children:[{
			path: 'index',
			meta:{title:'管理端首页'},
			component:()=> import("../views/admin/home.vue")
		},{
      path: 'messge',
      name:'adminMessge',
      meta:{title:'消息管理'},
      component: ()=> import("../views/admin/messge.vue")
    }]
	},{
		path: '/admin/login',
		meta:{title:'管理端'},
		component:()=> import("../views/admin/login.vue")
	},{
		path: '/admin/register',
		meta:{title:'管理端-注册'},
		component:()=> import("../views/admin/register.vue")
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})
const token={
	//客户（求职者）token
	home:'',
	//企业token
	enterprise:'',
	//管理员token
	admin:''
}
router.beforeEach((to, from, next) => {
	document.title = getPageTitle(to.meta.title);
	
	// 判断有没有登录
	token.home = localStorage.getItem('user_token');
	token.enterprise = localStorage.getItem('enterprise_token');
	token.admin = localStorage.getItem('admin_token');
	let hash = location.hash.split('/').splice(1,1)[0]
	// console.log(hash,token)
	if ([`/home/login`,`/home/register`].indexOf(to.path)!=-1 || [`/enterprise/login`,`/enterprise/register`].indexOf(to.path)!=-1 || [`/admin/login`,`/admin/register`].indexOf(to.path)!=-1) {  //登录注册无需登录
		next();
	} else {
		switch(hash){
			case 'home':
				if(token.home === null || !token.home || token.home === 'null'){
					next(`/home/login`);
				}else{
					next();
				}
				break;
			case 'admin':
				if(token.admin === null || !token.admin || token.admin === "null"){
					next(`/admin/login`);
				}else{
					next();
				}
				break;
			case 'enterprise':
				if(token.enterprise === null || !token.enterprise || token.enterprise === 'null'){
					next(`/enterprise/login`);
				}else{
					next();
				}
				break;
			default:
				next(`/enterprise/login`);
		}
	}
	
 });


export default router